﻿//
// VKSL - Visual Knowledge in Second Life
// 2007(c) CC License by-nc-sa-2.5-it
//
// Authors:
//     JonnyBee Cioc    jonny@vulca.no
//     VisionRaymaker   vision.raymaker@vulca.no

// ===[ LICENSE ]=======================================

// License Creative Commons Attribution-Noncommercial-Share Alike 2.5 Italy
// http://creativecommons.org/licenses/by-nc-sa/2.5/it/

// ===[ CHANGE LOG ]====================================
integer version_major    	 =  1;
integer version_minor    	 =  0;
integer version_revision 	 = 13;

// 2007-12-07 v1.0.13: first revision debug dumper module
//

// ===[ CONSTANTS ]=====================================
integer PUBLIC  			 = TRUE;
string  SEPARATOR            = "|";
integer LINK_NUMBER          = LINK_SET;

// LINKED MESSAGE PUBLIC INTERFACE
// MODULE
integer MESSAGELINKED_RESET            = 0xFFFF;
integer MESSAGELINKED_MODULEREADY      = 0xFFFE;
integer MESSAGELINKED_VERSION          = 0xFFFD;
integer MESSAGELINKED_DEBUG            = 0xF000;

// ERROR
integer MESSAGELINKED_ERROR            = 0xE000;
integer MESSAGELINKED_LISTOVERFLOW     = 0xE001;
integer MESSAGELINKED_ILLEGALPARAM     = 0xE002;
integer MESSAGELINKED_TRUNCATEDMESSAGE = 0xE003;

// MENU DIALOG
integer MESSAGELINKED_DIALOG           = 0x2001;
integer MESSAGELINKED_DIALOGRESPONSE   = 0x2002;
integer PARAM_DIALOG_ID                = 0x2003;
integer PARAM_DIALOG_TITLE             = 0x2004;
integer PARAM_DIALOG_AVATAR            = 0x2005;
integer PARAM_DIALOG_CHANNEL           = 0x2006;
integer PARAM_DIALOG_BUTTON_ID         = 0x2007;

// HOVERTEXT
integer MESSAGELINKED_HOVERTEXT_SET    = 0x3000;
integer MESSAGELINKED_HOVERTEXT_UNSET  = 0x3001;
integer MESSAGELINKED_HOVERTEXT_TEXT   = 0x3002;
integer MESSAGELINKED_HOVERTEXT_COLOR  = 0x3003;
integer MESSAGELINKED_HOVERTEXT_ALPHA  = 0x3004;
integer PARAM_HOVERTEXT_TEXT           = 0x3005;
integer PARAM_HOVERTEXT_COLOR          = 0x3006;
integer PARAM_HOVERTEXT_ALPHA          = 0x3007;

// MAP
integer MESSAGELINKED_MAP              = 0xC000;
integer PARAM_MAP_NAME                 = 0xC001;
integer PARAM_MAP_OWNERNAME            = 0xC002;
integer PARAM_MAP_TYPE                 = 0xC003;
integer PARAM_MAP_NODECOUNT            = 0xC004;
integer PARAM_MAP_LINKCOUNT            = 0xC005;

// MOVER
integer MESSAGELINKED_MOVER_OFFSET     = 0xC100;

// commands
integer CMD_MAP_PARAMS       = 0x8000;
integer CMD_OBJECTREZ        = 0x8001;
integer CMD_MAPTYPE          = 0x8002;
integer CMD_NEWNODE          = 0x8003;
integer CMD_NEWLINK          = 0x8004;
integer CMD_NEWMOVER         = 0x8005;
integer CMD_TOUCH            = 0x8006;
integer CMD_LINKCHANGE       = 0x8007;
integer CMD_CUTBRANCH        = 0x8008;
integer CMD_CLEAR            = 0x8009;
integer CMD_CLEARMAP         = 0x800A;
integer CMD_SAVE             = 0x800B;
integer CMD_SAVED            = 0x800C;
integer CMD_NODENUMBER       = 0x800D;
integer CMD_MOVE             = 0x800E;
integer CMD_USER             = 0x800F;
integer CMD_LINKUSER         = 0x8010;
integer CMD_NAME             = 0x8011;

// command's params
integer PARAM_MAPKEY         = 0x8100;
integer PARAM_MAPNAME        = 0x8101;
integer PARAM_MAPTYPE        = 0x8102;
integer PARAM_MAPNODECOUNT   = 0x8103;
integer PARAM_MAPLINKCOUNT   = 0x8104;
integer PARAM_PARENTKEY      = 0x8105;
integer PARAM_USERKEY        = 0x8106;
integer PARAM_USERNAME       = 0x8107;
integer PARAM_MOVERKEY       = 0x8108;
integer PARAM_LINKKEY        = 0x8109;
integer PARAM_LINKNAME       = 0x810A;
integer PARAM_NODEKEY        = 0x810B;
integer PARAM_NODEID         = 0x810C;
integer PARAM_NODENAME       = 0x810D;
integer PARAM_POSITION       = 0x810E;
integer PARAM_TITLE          = 0x810F;
integer PARAM_MAP_NAME       = 0x8110;
integer PARAM_MAP_OWNERNAME  = 0x8111;
integer PARAM_MAP_TYPE       = 0x8112;


// menu ID
integer MENUID_SERVER_ALL      = 0x1000;
integer MENUID_SERVER_START    = 0x1001;
integer MENUID_SERVER_ADMIN    = 0x1002;
integer MENUID_SERVER_USER     = 0x1003;
integer MENUID_SERVER_RELEASE  = 0x1004;
integer MENUID_SERVER_REQUEST  = 0x1005;
integer MENUID_SERVER_PENDING  = 0x1007;
integer MENUID_SERVER_ALLOWED  = 0x1008;
integer MENUID_NODE_FIRSTNODE  = 0x1100;
integer MENUID_NODE_NEWNODE    = 0x1101;
integer MENUID_NODE_COLOR      = 0x1102;
integer MENUID_LINK            = 0x1200;
integer MENUID_LINK_COLOR      = 0x1201;

list    MAP;
key     link_key;

// ===[ UTILITY FUNCTIONS ]=======================================
sendML(integer messagelinked, string message)
{
    llMessageLinked(LINK_NUMBER, messagelinked, message, link_key);
}

string version() { return " v"+(string)version_major+"."+(string)version_minor+"."+(string)version_revision; }

string remapMessage(string message)
{
    integer index = llSubStringIndex(message, SEPARATOR);
    if(~index) {
        list params = llParseString2List(message, [SEPARATOR], []);
        list map_key= llList2ListStrided(MAP, 0, -1, 2);
        integer i = 0;
        integer l = llGetListLength(params);
        message = "";
        for(;i<l;++i)
        {
            index = llListFindList(map_key, [(integer)llList2String(params,i)]);
            if(~index)
                message += llList2String(MAP, (index<<1)+1) +"="+llList2String(params,++i)+"\n";
            else {
                if(i>0) message += ", ";
                message += llList2String(params, i);
            }
        }
    }
    return message;

}

// ===[ STATE ]===================================================

default
{
    state_entry()
    {
        link_key = llGetLinkKey(llGetLinkNumber());

        MAP = [
            MESSAGELINKED_RESET,          "MESSAGELINKED_RESET",
            MESSAGELINKED_MODULEREADY,    "MESSAGELINKED_MODULEREADY",
            MESSAGELINKED_VERSION,        "MESSAGELINKED_VERSION",
            MESSAGELINKED_DEBUG,          "MESSAGELINKED_DEBUG",
            MESSAGELINKED_ERROR,          "MESSAGELINKED_ERROR",
            MESSAGELINKED_LISTOVERFLOW,   "MESSAGELINKED_LISTOVERFLOW",
            MESSAGELINKED_DIALOG,         "MESSAGELINKED_DIALOG",
            MESSAGELINKED_DIALOGRESPONSE, "MESSAGELINKED_DIALOGRESPONSE",
            MESSAGELINKED_LISTEN ,        "MESSAGELINKED_LISTEN",
            MESSAGELINKED_DEBUG,          "MESSAGELINKED_DEBUG",
            MESSAGELINKED_ERROR,          "MESSAGELINKED_ERROR",
            MESSAGELINKED_LISTOVERFLOW,   "MESSAGELINKED_LISTOVERFLOW",
            MESSAGELINKED_ILLEGALPARAM,   "MESSAGELINKED_ILLEGALPARAM",
            MESSAGELINKED_TRUNCATEDMESSAGE,"MESSAGELINKED_TRUNCATEDMESSAGE",
            MESSAGELINKED_HOVERTEXT_SET,  "MESSAGELINKED_HOVERTEXT_SET",
            MESSAGELINKED_HOVERTEXT_UNSET,"MESSAGELINKED_HOVERTEXT_UNSET",
            MESSAGELINKED_HOVERTEXT_TEXT, "MESSAGELINKED_HOVERTEXT_TEXT",
            MESSAGELINKED_HOVERTEXT_COLOR,"MESSAGELINKED_HOVERTEXT_COLOR",
            MESSAGELINKED_HOVERTEXT_ALPHA,"MESSAGELINKED_HOVERTEXT_ALPHA",
            MESSAGELINKED_MOVER_OFFSET,   "MESSAGELINKED_MOVER_OFFSET",
            MESSAGELINKED_MAP,            "MESSAGELINKED_MAP",
            CMD_MAP_PARAMS,               "CMD_MAP_PARAMS",
            CMD_OBJECTREZ,                "CMD_OBJECTREZ",
            CMD_MAPTYPE,                  "CMD_MAPTYPE",
            CMD_NEWNODE,                  "CMD_NEWNODE",
            CMD_NEWLINK,                  "CMD_NEWLINK",
            CMD_NEWMOVER,                 "CMD_NEWMOVER",
            CMD_TOUCH,                    "CMD_TOUCH",
            CMD_LINKCHANGE,               "CMD_LINKCHANGE",
            CMD_CUTBRANCH,                "CMD_CUTBRANCH",
            CMD_CLEAR,                    "CMD_CLEAR",
            CMD_CLEARMAP,                 "CMD_CLEARMAP",
            CMD_SAVE,                     "CMD_SAVE",
            CMD_SAVED,                    "CMD_SAVED",
            CMD_NODENUMBER,               "CMD_NODENUMBER",
            CMD_MOVE,                     "CMD_MOVE",
            CMD_USER,                     "CMD_USER",
            CMD_LINKUSER,                 "CMD_LINKUSER",
            CMD_NAME,                     "CMD_NAME",
            PARAM_MAPKEY,		          "PARAM_MAPKEY",
            PARAM_MAPNAME,		          "PARAM_MAPNAME",
            PARAM_MAPTYPE,		          "PARAM_MAPTYPE",
            PARAM_MAPNODECOUNT,  	      "PARAM_MAPNODECOUNT",
            PARAM_MAPLINKCOUNT,	          "PARAM_MAPLINKCOUNT",
            PARAM_PARENTKEY,	          "PARAM_PARENTKEY",
            PARAM_USERKEY,		          "PARAM_USERKEY",
            PARAM_USERNAME,		          "PARAM_USERNAME",
            PARAM_MOVERKEY,		          "PARAM_MOVERKEY",
            PARAM_LINKKEY,		          "PARAM_LINKKEY",
            PARAM_LINKNAME,		          "PARAM_LINKNAME",
            PARAM_NODEKEY,		          "PARAM_NODEKEY",
            PARAM_NODEID,		          "PARAM_NODEID",
            PARAM_NODENAME,		          "PARAM_NODENAME",
            PARAM_POSITION,		          "PARAM_POSITION",
            PARAM_TITLE,		          "PARAM_TITLE",
            PARAM_DIALOG_ID,              "PARAM_DIALOG_ID",
            PARAM_DIALOG_TITLE,           "PARAM_DIALOG_TITLE",
            PARAM_DIALOG_AVATAR,          "PARAM_DIALOG_AVATAR",
            PARAM_DIALOG_CHANNEL,         "PARAM_DIALOG_CHANNEL",
            PARAM_DIALOG_BUTTON_ID,       "PARAM_DIALOG_BUTTON_ID",
            PARAM_HOVERTEXT_TEXT,         "PARAM_HOVERTEXT_TEXT",
            PARAM_HOVERTEXT_COLOR,        "PARAM_HOVERTEXT_COLOR",
            PARAM_HOVERTEXT_ALPHA,        "PARAM_HOVERTEXT_ALPHA",
            MENUID_SERVER_ALL,            "MENUID_SERVER_ALL",
            MENUID_SERVER_START,          "MENUID_SERVER_START",
            MENUID_SERVER_ADMIN,          "MENUID_SERVER_ADMIN",
            MENUID_SERVER_USER,           "MENUID_SERVER_USER",
            MENUID_SERVER_RELEASE,        "MENUID_SERVER_RELEASE",
            MENUID_SERVER_REQUEST,        "MENUID_SERVER_REQUEST",
            MENUID_SERVER_PENDING,        "MENUID_SERVER_PENDING",
            MENUID_SERVER_ALLOWED,        "MENUID_SERVER_ALLOWED",
            MENUID_NODE_FIRSTNODE,        "MENUID_NODE_FIRSTNODE",
            MENUID_NODE_NEWNODE,          "MENUID_NODE_NEWNODE",
            MENUID_NODE_COLOR,            "MENUID_NODE_COLOR",
            MENUID_LINK,                  "MENUID_LINK",
            MENUID_LINK_COLOR,            "MENUID_LINK_COLOR"
            ];

        sendML(MESSAGELINKED_MODULEREADY, llGetScriptName()+" "+version());
    }

    link_message(integer sender_number, integer number, string message, key id)
    {
        if(number==MESSAGELINKED_DEBUG) {
            if(PUBLIC)
                llSay(0,   "DEBUG("+llKey2Name(id)+"): "+ message);
            else
                llOwnerSay("DEBUG("+llKey2Name(id)+"): "+ message);
        }
        else if(number==MESSAGELINKED_RESET)
            llResetScript();
    }
}